package com.lanou.renrenxin.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.easemob.EMCallBack;
import com.easemob.chat.EMChatManager;
import com.easemob.chat.EMGroupManager;
import com.lanou.renrenxin.R;
import com.lanou.renrenxin.tools.RenRenXinHelper;
import com.lanou.renrenxin.base.BaseActivity;
import com.lanou.renrenxin.base.BaseApplication;
import com.lanou.renrenxin.tools.LogUtils;
import com.parse.ParseAnalytics;

/**
 * Created by dllo on 16/3/1.
 */
public class LoginActivity extends BaseActivity implements View.OnClickListener {
    private TextView textViewOne, textViewTwo;
    private Button btn_register, btn_login;
    private EditText userNameEt, passWordEt;
    private String userName;
    private Handler handler;
    private boolean autoLogin = false;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ParseAnalytics.trackAppOpenedInBackground(getIntent());
        // 如果登录成功过，直接进入主页面
        if (RenRenXinHelper.getInstance().isLoggedIn()) {
            autoLogin = true;
            Intent intent = new Intent(LoginActivity.this, MainActivity.class);
            intent.putExtra("LOGIN_USER_NAME", EMChatManager.getInstance().getCurrentUser());
            startActivity(intent);
            return;
        }

        setContentView(R.layout.activity_login);

        textViewOne = (TextView) findViewById(R.id.renRenXin);
        textViewTwo = (TextView) findViewById(R.id.jiShiTongXun);
        textViewOne.setRotationY(20);
        textViewTwo.setRotationY(-20);
        //登录、注册按钮监听事件
        btn_register = (Button) findViewById(R.id.btn_register);
        btn_login = (Button) findViewById(R.id.btn_logIn);
        btn_register.setOnClickListener(this);
        btn_login.setOnClickListener(this);

        userNameEt = (EditText) findViewById(R.id.userName_login);
        passWordEt = (EditText) findViewById(R.id.passWord_login);

        Intent intent = getIntent();
        String userNameqqqq = intent.getStringExtra("name");
        userNameEt.setText(userNameqqqq);

        //登陆
        handler = new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(Message msg) {
                if (msg.what == BaseApplication.LOGIN_SUCCESS) {
                    Intent intentLogin = new Intent(LoginActivity.this, MainActivity.class);
                    intentLogin.putExtra("LOGIN_USER_NAME", userName);
                    startActivity(intentLogin);
                    finish();
                }
                return false;
            }
        });

        // 如果用户名改变，清空密码
        userNameEt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int before, int count) {
                passWordEt.setText(null);
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    private void login() {
        userName = userNameEt.getText().toString();
        LogUtils.e("DEBUG", userName);
        final String password = passWordEt.getText().toString();
        EMChatManager.getInstance().login(userName, password, new EMCallBack() {
            @Override
            public void onSuccess() {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        EMGroupManager.getInstance().loadAllGroups();
                        EMChatManager.getInstance().loadAllConversations();
                        handler.sendEmptyMessage(BaseApplication.LOGIN_SUCCESS);
                        Log.d("main", "登陆聊天服务器成功！");
                        //与服务器进行交互
                        interactiveService();
                    }
                });
            }

            @Override
            public void onError(int code, String message) {
                Log.d("main", "登陆聊天服务器失败！");
            }

            @Override
            public void onProgress(int i, String s) {

            }
        });
    }

    //服务器交互,判断服务器中知否存在该用户,如果不存在则存入服务器
    private void interactiveService() {
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_register:
                Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
                startActivity(intent);
                break;
            case R.id.btn_logIn:
                if (userNameEt.getText().length() > 0 && passWordEt.getText().length() > 0) {
                    login();
                } else {
                    Toast.makeText(LoginActivity.this, "用户名或密码不能为空", Toast.LENGTH_SHORT).show();
                }
                break;
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (autoLogin) {
            return;
        }
    }
}
